package com.hanthink.system.service.impl;

import com.hanthink.common.exception.CustomException;
import com.hanthink.common.utils.DateUtils;
import com.hanthink.common.utils.StringUtils;
import com.hanthink.system.domain.EmQualityWeighing;
import com.hanthink.system.domain.EmXflh;
import com.hanthink.system.domain.label.LabelVerification;
import com.hanthink.system.mapper.EmQualityWeighingMapper;
import com.hanthink.system.mapper.EmXflhMapper;
import com.hanthink.system.service.IEmQualityWeighingService;
import com.hanthink.system.service.IEmXflhService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 质量称重Service业务层处理
 * 
 * @author tzy
 * @date 2023-09-04
 */
@Service
public class EmXflhServiceImpl implements IEmXflhService
{
    @Autowired
    private EmXflhMapper emXflhMapper;

    /**
     * 查询质量称重
     * 
     * @param id 质量称重ID
     * @return 质量称重
     */
    @Override
    public EmXflh selectEmQualityWeighingById(Long id)
    {
        return emXflhMapper.selectEmQualityWeighingById(id);
    }

    /**
     * 查询质量称重列表
     * 
     * @param emQualityWeighing 质量称重
     * @return 质量称重
     */
    @Override
    public List<EmXflh> selectEmQualityWeighingList(EmXflh emQualityWeighing)
    {
        return emXflhMapper.selectEmQualityWeighingList(emQualityWeighing);
    }

    /**
     * 新增质量称重
     * 
     * @param emQualityWeighing 质量称重
     * @return 结果
     */
    @Override
    public int insertEmQualityWeighing(EmXflh emQualityWeighing)
    {
        emQualityWeighing.setCreateTime(DateUtils.getNowDate());
        return emXflhMapper.insertEmQualityWeighing(emQualityWeighing);
    }

    /**
     * 修改质量称重
     * 
     * @param emQualityWeighing 质量称重
     * @return 结果
     */
    @Override
    public int updateEmQualityWeighing(EmXflh emQualityWeighing)
    {
        return emXflhMapper.updateEmQualityWeighing(emQualityWeighing);
    }

    /**
     * 批量删除质量称重
     * 
     * @param ids 需要删除的质量称重ID
     * @return 结果
     */
    @Override
    public int deleteEmQualityWeighingByIds(Long[] ids)
    {
        return emXflhMapper.deleteEmQualityWeighingByIds(ids);
    }

    /**
     * 删除质量称重信息
     * 
     * @param id 质量称重ID
     * @return 结果
     */
    @Override
    public int deleteEmQualityWeighingById(Long id)
    {
        return emXflhMapper.deleteEmQualityWeighingById(id);
    }

    @Override
    public EmXflh selectOneByEquipKey(String key) {
        return emXflhMapper.selectOneByEquipKey(key);
    }

    @Override
    public String importPlan(List<EmXflh> labelList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(labelList) || labelList.size() == 0) {
            throw new CustomException("导入计划数据不能为空！");
        }
        //成功数据
        int successNum = 0;
        //失败数据
        int failureNum = 0;
        //重复数据
        int duplicateNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        StringBuilder duplicateMsg = new StringBuilder();
        int currentRowNum = 0;
        for (EmXflh label : labelList) {
            //label.setStatus("1");
            currentRowNum++;
            try {
                // 校验数据信息
//                if (StringUtils.isEmpty(label.getCustomer())) {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、第" + currentRowNum + "行数据客户名称不能为空");
//                    continue;
//                } else if (StringUtils.isEmpty(label.getProduction())) {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、第" + currentRowNum + "行数据产品名称不能为空");
//                    continue;
//                } else if (StringUtils.isNull(label.getFrontlabel()) || !StringUtils.isNumeric(label.getFrontlabel())) {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、第" + currentRowNum + "行数据前标签标签号不能为空且只能为数字类型");
//                    continue;
//                } else if (StringUtils.isEmpty(label.getBacklabel()) || !StringUtils.isNumeric(label.getBacklabel())) {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、第" + currentRowNum + "行数据背标签标签号不能为空且只能为数字类型");
//                    continue;
//                } else if (StringUtils.isEmpty(label.getStatus()) || !StringUtils.isNumeric(label.getStatus())) {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、第" + currentRowNum + "行数是否启用状态不能为空且只能为数字类型");
//                    continue;
//                }
                label.setEquipKey(label.getEquipKey().replaceAll("\\s+", ""));
                label.setLeftStation(label.getLeftStation().replaceAll("\\s+", ""));
                label.setRightStation(label.getRightStation().replaceAll("\\s+", ""));
                label.setCommonParts(label.getCommonParts().replaceAll("\\s+", ""));
                label.setAuxiliaryEquip(label.getAuxiliaryEquip().replaceAll("\\s+",  ""));
                label.setCreatedBy(label.getCreatedBy().replaceAll("\\s+",  ""));
                EmXflh queryLabel = new EmXflh();
                queryLabel.setEquipKey(label.getEquipKey());
                queryLabel.setLeftStation(label.getLeftStation());
                queryLabel.setRightStation(label.getRightStation());
                queryLabel.setCommonParts(label.getCommonParts());
                queryLabel.setAuxiliaryEquip(label.getAuxiliaryEquip());
                queryLabel.setCreatedBy(label.getCreatedBy());
                //queryLabel.setIsdelete("0");
                List<EmXflh> labels = emXflhMapper.getList(queryLabel);
                if(labels.size() > 0 && isUpdateSupport){
                    EmXflh returnLabel = labels.get(0);
                    label.setId(returnLabel.getId());
                    this.updateEmQualityWeighing(label);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、标签数据 " + label.getEquipKey() + " 更新成功");
                }else if(labels.size() > 0 && !isUpdateSupport){
                    //数据重复
                    duplicateNum++;
                    duplicateMsg.append("<br/>" + duplicateNum + "、第" + currentRowNum + "行数据已存在，请勾选更新按钮更新已经存在的标签数据");
                }else {
                    this.insertEmQualityWeighing(label);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、标签数据 " + label.getEquipKey() + " 导入成功");
                }

            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、标签数据" + label.getEquipKey() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                //log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }else if (duplicateNum > 0) {
            if(successNum>0){
                duplicateMsg.insert(0, "共 " + successNum + " 条数据导入成功！其中"+ duplicateNum + " 条数据导入失败，错误如下：");
            }else{
                duplicateMsg.insert(0, "很抱歉，共 " + duplicateNum + " 条数据导入失败，错误如下：");
            }
            throw new CustomException(duplicateMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
